const { productService,appoint,sequelize,memberInfo } = require('../../../dbConfig/index')
const {Op, where} = require('sequelize')
// 获取当前店家的预约
const getMerchantApponit = async (ctx, next) => {
    const { queryDate } = ctx.request.body; // 获取商户ID和可选的查询日期
    const merchantId = ctx.state.user_id

    try {
        // 构建查询条件
        const whereConditions = {
            merchant_id: merchantId, // 商户ID
            appoint_state: {
                [Op.or]: ['1', '2']
            } // 只查询有效的预约状态
        };

        // 如果提供了查询日期，则添加日期条件
        if (queryDate) {
            delete whereConditions.appoint_state
            whereConditions.appointment_time = {
                [Op.like]:'%'+queryDate+'%'
            };
        }

        // 查询预约数据
        const appointments = await appoint.findAll({
            where: whereConditions,
            order: [['appointment_time', 'ASC']],
            include: [
                {
                    model: productService,
                    as: 'productService', // 使用定义的别名
                    required: true,
                    attributes: ['Id', 'service_name', 'service_price'] // 选择需要的字段
                },
                {
                    model: memberInfo,
                    required: true,
                    attributes: ['user_id','full_name','head_img'] // 选择需要的字段
                }
            ]
        });

        // 返回结果
        ctx.body = {
            code: 200,
            data: appointments,
            msg: '成功'
        };
    } catch (error) {
        console.error('获取商户预约信息失败:', error);
        ctx.body = {
            code: 500,
            msg: '获取商户预约信息失败'
        };
    }
}
module.exports = {
	getMerchantApponit
}